﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace tupleview
{
    class AppFacade
    {
        protected AppFacade() { }
        private static AppFacade _instance;
        public static AppFacade Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AppFacade();
                }
                return _instance;
            }
        }
        connect_handle m_client_connect;
        connect_handle m_client_disconnect;
        connect_handle m_server_connect;
        connect_handle m_server_disconnect;
        message_handle m_client_message;
        message_handle m_server_message;
        error_handle m_client_error;
        error_handle m_server_error;
        public void Start()
        {
            m_client_connect = new connect_handle(on_client_connect);
            m_client_disconnect = new connect_handle(on_client_disconnect);
            m_server_connect = new connect_handle(on_server_connect);
            m_server_disconnect = new connect_handle(on_server_disconnect);
            m_client_message = new message_handle(on_client_message);
            m_server_message = new message_handle(on_server_message);
            m_client_error = new error_handle(on_client_error);
            m_server_error = new error_handle(on_server_error);
            net_tuple.set_on_client_connect(m_client_connect);
            net_tuple.set_on_client_disconnect(m_client_disconnect);
            net_tuple.set_on_client_message(m_client_message);
            net_tuple.set_on_client_error(m_client_error);
            net_tuple.set_on_server_connect(m_server_connect);
            net_tuple.set_on_server_disconnect(m_server_disconnect);
            net_tuple.set_on_server_message(m_server_message);
            net_tuple.set_on_server_error(m_server_error);
            net_tuple.set_remote("127.0.0.1", 8001);
            net_tuple.bind_to(9999);
            Console.WriteLine("已监听9999");
        }

        void on_client_connect(int id)
        {
            Console.WriteLine("on_client_connect:" + id);
        }

        void on_client_disconnect(int id)
        {
            Console.WriteLine("on_client_disconnect:" + id);
        }

        int on_client_message(int id, byte[] data, int len)
        {
            Console.WriteLine("on_client_message:" + id + " len:" + len);
            net_tuple.send_to_server(id, data, len);
            return len;
        }

        void on_client_error(int id, int error_code)
        {
            Console.WriteLine("on_client_error:" + id + " error_code:" + error_code);
        }

        void on_server_connect(int id)
        {
            Console.WriteLine("on_server_connect:" + id);
        }

        void on_server_disconnect(int id)
        {
            Console.WriteLine("on_server_disconnect:" + id);
        }

        int on_server_message(int id, byte[] data, int len)
        {
            Console.WriteLine("on_server_message:" + id + "len:" + len);
            net_tuple.send_to_client(id, data, len);
            return len;
        }

        void on_server_error(int id, int error_code)
        {
            Console.WriteLine("on_server_error:" + id + " error_code:" + error_code);
        }
    }
}
